﻿using System;
using System.ComponentModel;
using Microsoft.Xna.Framework;

namespace SceneManager.Components
{
	public abstract class Component : IDisposable, INotifyPropertyChanged
	{
		#region Events

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion

		#region Constructors

		public Component(ComponentCollection parent)
		{
			ParentCollection = parent;

			foreach (var attribute in GetType().GetCustomAttributes<RequireComponentAttribute>(false))
			{
				var requiredComponentType = (attribute as RequireComponentAttribute).ComponentType;
				if (!ParentCollection.IsDefined(requiredComponentType))
				{
					throw new MissingComponentException(requiredComponentType);
				}
			}
		}

		#endregion

		#region Properties

		public ComponentCollection ParentCollection { get; set; }

		public GameObject GameObject
		{
			get
			{
				return ParentCollection.GameObject;
			}
		}

		public Scene Scene
		{
			get
			{
				return GameObject.Scene;
			}
		}

		#endregion

		#region Methods

		public void Dispose()
		{
			ParentCollection = null;
		}

		public virtual void LoadContent()
		{
		}

		public virtual void UnloadContent()
		{
		}

		public virtual void Update(GameTime gameTime)
		{
		}

		public virtual void Draw(GameTime gameTime)
		{
		}

		protected void PropertyHasChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}

		#endregion
	}
}